// pages/news/newsList/newsList.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    newsArr: [],
    pageNum: '1', //分页
    resultData: [], //数据源
    imgURL: app.globalData.imgURL, //图片总路径
    url: 'buyerpurinfo/queryAllOrdersListByUser',
    orderSData: [],
    showViewId: 2,
    noOrdersMsg: '未查询到订单信息~~',
    isLogin: 0,
  },
  showOrderDetail: function(e) {
    var that = this;
    wx.navigateTo({
      url: '../buysDetail/buysDetail?id=' + e.currentTarget.id + "&openId=" + that.data.openId + "&type=" + that.data.customerType +"&entrance=buyList&showReleaseBtn=show&toReleaseType=buyer",
    })

  },

  showResSeller:function(e){
    var that = this;
    wx.navigateTo({
      url: '../resProductList/resProductList?b_id=' + e.currentTarget.id + "&openId=" + that.data.openId ,
    })
  },

  //新闻点击
  newsListClick: function(e) {
    var that = this;
    console.log(e.currentTarget.dataset.url);
    app.post('news/getCounts', {
      id: e.currentTarget.id
    }).then((res) => {
      console.log('success');
      console.log(res);
      wx.navigateTo({
        url: '../newsDetail/newsDetail?url=' + e.currentTarget.dataset.url,
      })
    }).catch((errMsg) => {
      console.log('fail');
      console.log(errMsg + '失败');
    })
  },
  toRelease: function(e) {
    wx.navigateTo({
      url: '../releaseInfo/releaseInfo',
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  //请求数据
  requestData: function() {
    var that = this;

    app.post(that.data.url, {
      pageNum: that.data.pageNum,
      openId: that.data.openId
    }).then((res) => {
      console.log('success');
      console.log(res);
      if (res.code == 0) {
        if (res.resultData.length > 0) {
          that.setData({
            orderSData: that.data.orderSData.concat(res.resultData),
            showViewId: 1,
          })
        }

      } else {
        wx.showModal({
          title: '温馨提示',
          content: res.msg,
          showCancel: false,
        })
      }
      console.log("订单数量：" + this.data.orderSData.length);
    }).catch((errMsg) => {
      console.log('fail');
      console.log(errMsg + '失败');
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    if (wx.getStorageSync('userInfo')) {
      that.setData({
        userInfo: wx.getStorageSync('userInfo'), //获取用户信息
        openId: wx.getStorageSync('userInfo').userId, //获取openId
        orderSData: [],
        pageNum: 1,
        isLogin: 1
      });
      that.requestData();
    } else {
      that.setData({
        noOrdersMsg: '请先登录！！'
      })
      wx.showModal({
        title: '提示',
        content: '请先登录！！',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            wx.reLaunch({
              url: '../../user/userFList/userFList',
            })
          }
        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    that.setData({
      orderSData: [],
      pageNum: '1', //分页
      url: 'buyerpurinfo/queryAllOrdersList',
    })
    wx.showNavigationBarLoading();
    setTimeout(function() {
      that.requestData();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
    }, 2000);

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    console.log('加载更多');
    var that = this;
    that.data.pageNum++;
    console.log(that.data.pageNum);
    that.requestData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})